<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/27 0027
// +----------------------------------------------------------------------
// | Description:  
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\AttachMent;
use app\admin\service\UploadFileService;
use app\common\controller\AdminBaseController;
use Jrk\Tool;
use think\facade\Db;

class Common extends AdminBaseController
{

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }


    /**
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/27 0027
     * @describe:
     */
    public function changeStatus()
    {
        if (IS_POST) {
            try {
                $data = $this->request->post();
                if (empty($data['table'])) {
                    return parent::JsonReturn("表名不能为空", 0);
                }
                $table = $data['table'] ? $data['table'] : "";
                $res = Db::name("{$table}")->where(["id" => (int)$data['id']])->update([$data['field'] => $data['status']]);
                if ($res !== false) {
                    return parent::JsonReturn("操作成功");
                } else {
                    return parent::JsonReturn("操作失败", 0);
                }

            } catch (\Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }
    }


    /**
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:下载 excel文件
     */
    public function download(){
        $param=$this->request->param();
        $file_name=$param['fileName'];
        $file_path=app()->getRootPath().'public/'.$param['fileName'];
        if(!file_exists($file_path)){
            $this->error("文件不存在");exit;
        }
        $file1=fopen($file_path,"r");
        Header("Content-type: application/octet-stream;charset=utf-8");
        Header("Accept-Ranges: bytes");
        header("Content-Length: " . filesize($file_path));//文件大小
        header('Content-Disposition:attachment;filename='.$file_name.'');
        ob_clean();
        flush();
        echo fread($file1,filesize($file_path)); //输出文件大小到浏览器
        fclose($file1);
        @unlink($file_path); //删除文件
        exit();
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:头像上传
     */
    public function UpImg(){
        $ok= self::UpImages("adminuser/avatar");
        if (is_array($ok)){
            return parent::JsonReturn("上传成功",1,'',$ok);
        }else{
            return parent::JsonReturn($ok,0);
        }
    }


    /**
     * @return array|false|string|\think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:webupload上传素材图片
     */
    public function upWebupload(){
        $res= self::UpImages("attachment/images");
        if (is_array($res)){
            return json_encode($res['dir']);
        }else{
            return $res;
        }
    }


    /**
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:上传文件
     */
    public function UpFile(){
       $file= self::UpFiles("attachment/files");
        if ($file["status"]==true){
            return parent::JsonReturn("上传成功",1,'',$file);
        }else{
            return ['code' => 0, 'msg' => '上传失败'];
        }
    }


    /**
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe: 弹出层上传图片
     */
    public function UpImagesList(){
        $res= self::UpImages("attachment/images");
        if (is_array($res)){
            return parent::JsonReturn("上传成功",1,'',$res);
        }else{
            return parent::JsonReturn($res,0);
        }
    }



    /**
     * @return \think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:文章封面
     */
    public function UpArticlePic(){
        $res= self::UpImages("article/images");
        if (is_array($res)){
            return parent::JsonReturn("上传成功",1,'',$res);
        }else{
            return parent::JsonReturn($res,0);
        }
    }


    /**
     * @return array|\think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:Editormd 图片上传
     */
    public function uploadEditormdImages(){
        $file= self::UpImages("editormd/images",1,"editormd-image-file");
        if (is_array($file)){
            $data = ['success' => 1, 'message' => "上传成功",'url'=>$file['dir'],"time"=>time()];
            return json($data);
        }else{
            return json(['success' => 0, 'message' => $file]);
        }
    }


  
    /**
     * @param $path  //保存的路径
     * @param int $is_save  是否保存到数据库
     * @param string $fileName
     * @return \think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:公共图片上传方法
     */
    protected static function UpImages($path,$is_save=1,$fileName="file"){
        if (empty($path)) $path="common/images";
        $res= UploadFileService::instance()
            ->setImageValidateArray(['fileExt'=>['jpg', 'jpeg', 'png', 'gif'],'fileMime' => ['image/jpeg', 'image/gif', 'image/png']])
            ->setUploadPath($path)->image($fileName);
        if (is_array($res)){
             if ($is_save==1){
                 AttachMent::attachmentAdd($res['name'],$res['size'],$res['type'],$res['dir'],$res['thumb_path'],$res['image_type'],$path,$res['ext']);
             }
        }
         return $res;
    }



    /**
     * @param $path
     * @param int $is_save
     * @param string $fileName
     * @return mixed|object
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:上传附件
     */
    protected static function UpFiles($path,$is_save=1,$fileName="file"){
        if (empty($path)) $path="common/files";
        $res=UploadFileService::instance()
        ->setImageValidateArray(['fileExt'=>['txt', 'xls', 'xlsx', 'doxc','doc']])
        ->setUploadPath($path)->file($fileName);
        $res=Tool::object_to_array($res);
        if ($res["status"]==true){
            if ($is_save==1){
                AttachMent::attachmentAdd($res['name'],$res['size'],$res['type'],$res['dir'],$res['thumb_path'],$res['image_type'],$path,$res['ext']);
            }
        }
        return $res;
    }





}